#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int kind = 0;
int find_flag= 0;

int main(){
	get_modify_line();
    notify_status(); //get file and folder num
}

int get_modify_line()
{
	system("grep -v MODIFY bak-even |wc -l > ama_ca");
	system("grep -rn MODIFY bak-even |awk -F \":\" \'{print $2}\' > ama_mo");
	char notify_tmp[16];
	FILE *fp1=fopen("ama_ca","r");
	fgets(notify_tmp,16,fp1);
	int notify1=atoi(notify_tmp);
	printf("notify1 is %d\n",notify1);
	fclose(fp1);
	unlink("ama_ca");
	system("cat ama_mo |wc -l > mo_lines");
	FILE *fp3=fopen("mo_lines","r");
	char mo_line[16];
	fgets(mo_line,16,fp3);
	printf("mo_line is %d\n",atoi(mo_line));
	fclose(fp3);
	unlink("mo_lines");
	FILE *fp2=fopen("ama_mo","r");
	if(fp2==NULL) return 0;
	char line[1024];
	char last_line[1024];
	int i;
	int count=0;
	for(i=0;i<atoi(mo_line);i++)
	{
		fgets(line,1024,fp2);
		printf("line is %s",line);
		if(strcmp(last_line," ") !=0 && (strcmp(line,last_line) != 0))
			count ++;
		strncpy(last_line,line,1024);
	}
		fclose(fp2);
		printf("count is %d\n",count);
		unlink("ama_mo");
		return count;
}

int notify_status()
{
    char line[1024] = {0};
    char ** token;
    char cmd[256] = {0};
	system("echo -n > fileline");
	system("echo -n > dirline");
    FILE * fp_bk = fopen("bak-even", "r");
    if(fp_bk == NULL)
    {
        printf("open file error\n");
        return -1;
    }
    while (!feof(fp_bk))
    {
        memset(line, 0, sizeof(line));
        char * pc = fgets(line, 1024, fp_bk);
        if(pc == NULL)
        {
            printf("pc == NULL errno: %s\n", strerror(errno));
            break;
        }
        printf("while line:%s", line);
        int len = strlen(line); //drop '\n'
        line[len - 1] = '\0';
        token = (char **)malloc(4 * sizeof(char *));
        if(readline(line, token) == -1)
        {
            fclose(fp_bk);
            free(token);
            return -1;
        }
        printf("token[0]:%s,token[1]:%s,token[2]:%s,kind:%d\n", token[0], token[1], token[2], kind);
        char bkline[1024] = {0};
        sprintf(bkline, "%s%s", token[0], token[2]);
        if(kind == 0)
        {
            sprintf(cmd, "echo %s >> fileline", bkline);
            system(cmd);
        }
        else
        {
            sprintf(cmd, "echo %s >> dirline", bkline);
            system(cmd);
        }
    }

    fclose(fp_bk);
    free(token);
    get_num("file", "fileline");
    get_num("dir", "dirline");
	char buf_f[16]={0};
	char buf_d[16]={0};
	FILE *str1=popen("cat file_num","r");
	fread(buf_f,sizeof(char),sizeof(buf_f),str1);
	pclose(str1);
	printf("file num:%s",buf_f);
	FILE *str2=popen("cat dir_num","r");
	fread(buf_d,sizeof(char),sizeof(buf_d),str2);
	pclose(str2);
	printf("folder num:%s",buf_d);
    return 0;
}

int readline(char * line, char ** token)
{
    char * p;
    int i = 0;
    for(i = 0; i < 3; i++)
    {
        if(!i)
        {
            p = strtok(line, "|");
            if(p)
                token[i] = p;
            else
                return -1;
        }
        else
        {
            p = strtok(NULL, "|");
            if(p)
                token[i] = p;
        }
    }
    if(strstr(token[1], "ISDIR"))
        kind = 1;
    else
        kind = 0;
    return 0;
}

int get_num(char *type, char *bkfp)
{
    char line1[1024] = {0};
    char line2[1024] = {0};
	char cmd[256]={0};
    FILE * fp_t ;

    fp_t = fopen(bkfp, "r");
    if(fp_t == NULL)
		return -1;
	fgets(line1, 1024, fp_t);
//	printf("line1:%s", line1);
	int len1=strlen(line1);
	line1[len1-1]='\0';//drop '\n'
	memset(cmd,0,256);
	sprintf(cmd,"echo '%s' > %scount_line",line1,type);
	system(cmd);
	while(fgets(line2, 1024, fp_t))
	{
	//	printf("line2:%s", line2);
		int len2=strlen(line2);
		line2[len2-1]='\0';//drop '\n'
		if(strcmp(line1,line2) !=0){
			FILE *fp_l;
			char check_line[1024]={0};
			char typefile[1024]={0};
			sprintf(typefile,"%scount_line",type);
			fp_l=fopen(typefile,"r");
			while(fgets(check_line,1024,fp_l))
			{
	//			printf("check line=%s",check_line);
				int clen=strlen(check_line);
				check_line[clen-1]='\0';//drop '\n'
				if(strcmp(line2,check_line)==0){
	//				printf("find the same line\n");
					find_flag=1;
					break;
				}else{
					find_flag=0;
					continue;
				}
			}
				fclose(fp_l);
				if(find_flag==0){
					memset(cmd,0,256);
					sprintf(cmd,"echo '%s' >> %scount_line",line2,type);
					system(cmd);
				}
		}

	}
    fclose(fp_t);
	memset(cmd,0,256);
	sprintf(cmd,"sed -i '/^$/d' %scount_line",type); //delete null line
	system(cmd);
	memset(cmd,0,256);
	sprintf(cmd,"cat  %scount_line |wc -l > %s_num",type,type); //get line num
	system(cmd);
    return 0;

}

